#include "processortest.h"

/*Registry this TestSuite to theTestFactoryRegistry*/
CPPUNIT_TEST_SUITE_REGISTRATION (ProcessorTest);

void ProcessorTest::setUp() {
	SG_init();
	processorFixed=NULL;
	processorTrace=NULL;
	processorFixed = new Processor("procFixed", 1, 0.95);
	processorTrace = new Processor("procTrace", 2, "../data/traces/cputrace.0" , 0, "../data/traces/cputrace.0", "../data/traces/cputrace.0");	
}

void ProcessorTest::testConstructorException() { fprintf(stderr,">>> ProcessorTest::testConstructorException()\n");
  	Resource *p = NULL;
  	try {
		 p = new Processor("procTrace", 1, "../data/traces/NO_FILE" , 0, "../data/traces/NO_FILE", "../data/traces/NO_FILE");				
		CPPUNIT_FAIL("An exception should be throwed!! No file can be opened");
	} catch (std::runtime_error &e) {
	//	Pass
	}

}

void ProcessorTest::testGetName() { fprintf(stderr,">>> ProcessorTest::testGetName()\n");
	CPPUNIT_ASSERT_MESSAGE("Names do not match", (!strcmp("procFixed", processorFixed->getName())));
	CPPUNIT_ASSERT_MESSAGE("Names do not match", (!strcmp("procTrace", processorTrace->getName())));
	
	const char *namefixed = SG_getResourceName(processorFixed->getSGResource());
	const char *nametrace = SG_getResourceName(processorTrace->getSGResource());	
	CPPUNIT_ASSERT_MESSAGE("Names do not match", (!strcmp(namefixed, processorFixed->getName())));		
	CPPUNIT_ASSERT_MESSAGE("Names do not match", (!strcmp(nametrace, processorTrace->getName())));			
}

void ProcessorTest::testGetCpuSpeed() { fprintf(stderr,">>> ProcessorTest::testGetCpuSpeed()\n");
	Processor *p = new Processor("proc", 3, 0.95);
	CPPUNIT_ASSERT(p->getCpuSpeed()==3);
	delete p;	
	p = NULL;
	p = new Processor("procTrace", 2, "../data/traces/cputrace.0" , 0, "../data/traces/cputrace.0", "../data/traces/cputrace.0");	
	CPPUNIT_ASSERT(p->getCpuSpeed()==2);		
	 if (p!=NULL) delete p;
}

void ProcessorTest::testGetCpuAvailable() { fprintf(stderr,">>> ProcessorTest::testGetCpuAvailable()\n");
	long double fixed = 0.95;
	Processor *p = new Processor("proc", 3, 0.95);
//	printf("Fixed_Available=%Lf\n", p->getCpuAvailable(0));
	CPPUNIT_ASSERT(p->getCpuAvailable(0)==fixed);	
	CPPUNIT_ASSERT(p->getCpuAvailable(50)==fixed);	
	CPPUNIT_ASSERT(p->getCpuAvailable(100)==fixed);		
	delete p;
	p=NULL;
	double v = 0.898850;
	p = new Processor("procTrace", 2, "../data/traces/cputrace.0" , 0, "../data/traces/cputrace.0", "../data/traces/cputrace.0");	
	double trace = p->getCpuAvailable(110);	
//	printf("Trace_Available=%Lf", p->getCpuAvailable(110));	
	CPPUNIT_ASSERT(v==trace );		
	
//	SG_simulate(111,NULL, SG_VERBOSE);
//	printf("Trace_Available=%Lf", p->getCpuAvailable(-1));		
	
	 if (p!=NULL) delete p;	
}

void ProcessorTest::testGetType() { fprintf(stderr,">>> ProcessorTest::testGetType()\n");
	Resource *p = new Processor("proc", 3, 0.95);
	CPPUNIT_ASSERT(p->getType()==SG_HOST);
	delete p;
	
}

void ProcessorTest::testIsPerformanceConstant() { fprintf(stderr,">>> ProcessorTest::testIsPerformanceConstant()\n");
	Resource *p = new Processor("proc", 3, 0.95);
	CPPUNIT_ASSERT(p->isPerformanceConstant()==true);

	p = new Processor("procTrace", 2, "../data/traces/cputrace.0" , 0, "../data/traces/cputrace.0", "../data/traces/cputrace.0");	
	CPPUNIT_ASSERT(p->isPerformanceConstant()==false);

	delete p;
}

void ProcessorTest::testIsBusy() { fprintf(stderr,">>> ProcessorTest::testIsBusy()\n");
	Processor *p = new Processor("proc", 3, 0.95);
	CPPUNIT_ASSERT(p->isBusy()==false);

	SG_Task fakeTask = SG_newTask (SG_COMPUTATION, "task1", 100, NULL);
	SG_Task fakeTask2 = SG_newTask (SG_COMPUTATION, "task2", 100, NULL);

	SG_Task *tasks_scheduled = SG_getTasksScheduledOnResource( p->getSGResource());
	SG_Task *tasks_running = SG_getTasksRunningOnResource( p->getSGResource());	
	CPPUNIT_ASSERT(tasks_scheduled[0]==NULL);
	CPPUNIT_ASSERT(tasks_running[0]==NULL);
	
	SG_scheduleTaskOnResource(fakeTask, p->getSGResource());
	SG_scheduleTaskOnResource(fakeTask2, p->getSGResource());
	
	CPPUNIT_ASSERT(p->isBusy()==true);

	SG_simulate(-1, SG_ANY_TASK, SG_SILENT);

	CPPUNIT_ASSERT(p->isBusy()==true);	

	SG_simulate(-1, SG_ANY_TASK, SG_SILENT);

	CPPUNIT_ASSERT(p->isBusy()==false);	
	
	delete p;
}
void ProcessorTest::tearDown() {
	delete processorFixed;
	if (processorTrace!=NULL) delete processorTrace;
	SG_clear();
}
